<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/10 0010
 * Time: 下午 4:37
 */

namespace Admin\Controller;

use Think\Controller;

class ArticleController extends Controller {

    //创建模型对象
    private $_model = null;

    /**
     *
     * 初始化,遍历给每个页面
     */
    protected function _initialize() {

        //构建数据
        $meta_titles = array(
            'index' => '文章管理',
            'add' => '文章添加',
            'edit' => '文章修改',
            'delete' => '文章删除',
        );
        $meta_title = $meta_titles[ACTION_NAME];
        //发送数据
        $this->assign('meta_title', $meta_title);
        //创建模型对象
        $this->_model = D('Article');
    }

    public function index() {
        //构建sql条件发送到model
        $cond = array();
        //接受查询的值
        $key = I('get.keyword');
        if ($key) {
            $cond['name'] = array('like', '%' . $key . '%');
        };
        $p = I('get.p') ? I('get.p') : 1;
        //发送到模型
        $rows = $this->_model->getPageResult($cond, $p);
        $C = D('ArticleCategory')->select();
        $this->assign('c', $C);
        //发送数据
        $this->assign($rows);
        //渲染视图
        $this->display();
    }

    /**
     * 添加文章
     */
    public function add() {
        if (IS_POST) {
            //接受数据
            $data = $this->_model->create();
            //发送到模型处理
            $res = $this->_model->addRow($data);
            if ($res === false) {
                $this->error('添加失败');
            } else {
                $this->success('添加成功', U('index'));
            }
        } else {
            //获取分类表的数据
            $ArticleCategory_model = D('articleCategory');
            $ArticleCategory = $ArticleCategory_model->field('id,name')->where('status>-1')->select();
            //发送数据
            $this->assign('ArticleCategory', $ArticleCategory);
            $this->display();
        }
    }

    /**
     * 编辑页面
     */
    public function edit($id) {
        $ArticleContent = D('ArticleContent');
        $ArticleCategory = D('ArticleCategory');
        if (IS_POST) {
            //获取数据
            $data = $this->_model->create();
            $ArticleContent->where('id=$id')->save();
            //更新文章内容
            $content['content'] = I('post.content');
            $ArticleContent->where("article_id=$id")->save($content);
            //执行更新
            if ($this->_model->save($data) === false) {
                $this->error(get_error($this->_model->getError()));
            } else {
                $this->success('修改成功', U('index'));
            }
        } else {
            //获取文章内容
            $content = $ArticleContent->where("article_id=$id")->field('content')->select();
            //获取数据
            $rows = $this->_model->find($id);
            //获取分类信息发送
            $res = $ArticleCategory->where('status>-1')->select();
            $this->assign('res', $res);
            $this->assign('content', $content);
            //发送数据
            $this->assign('row', $rows);
            $this->display();
        }
    }

    public function del($id) {
        //发送到模型执行del方法
        $res = $this->_model->getDel($id);
        //判断是否逻辑删除成功
        if ($res) {
            $this->success('删除成功');
        } else {
            $this->error(get_error($this->_model->getError()));
        }
    }

}
